import SwiftUI
import SwiftData

// AddTradePage
// 添加交易意向

struct AddTradePage: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var loginInfo: [LoginInfo]
    
    @Environment(\.dismiss) private var dismiss
    
    @State private var wantPriceInput: String = ""
    @State private var localtionInput: String = ""
    @State private var commentInput: String = ""
    
    @StateObject private var addTradeRequester = TradeAddTradeRequester()
    
    var book: Book
    
    var body: some View {
        List {
            Section {
                HStack {
                    Text("*")
                        .foregroundColor(.red)
                    Text("期望价格")
                    Spacer()
                    TextField("¥", text: $wantPriceInput)
                        .keyboardType(.decimalPad)
                        .foregroundColor(.secondary)
                        .multilineTextAlignment(.trailing)
                        .onChange(of: wantPriceInput) {
                            if let first = wantPriceInput.first {
                                if(first != "¥") {
                                    wantPriceInput = "¥" + wantPriceInput
                                }
                            }
                        }
                }
                
                HStack {
                    Text("*")
                        .foregroundColor(.red)
                    Text("期望交易地点")
                    Spacer()
                    TextField("如：新北15公寓楼下", text: $localtionInput)
                        .foregroundColor(.secondary)
                        .multilineTextAlignment(.trailing)
                }
            }
            
            Section {
                HStack {
                    Text("留言")
                    Spacer()
                    TextEditor(text: $commentInput)
                        .foregroundColor(.secondary)
                        .multilineTextAlignment(.trailing)
                }
            }
            
            Section {
                Button {
                    addTradeRequester.request(userId: loginInfo.last?.userId ?? "",
                                              bookId: book.bookId,
                                              wantPrice: wantPriceInput,
                                              location: localtionInput,
                                              comment: commentInput)
                } label: {
                    HStack {
                        Spacer()
                        Text("确定")
                        Spacer()
                    }
                }
                .disabled(wantPriceInput == "" || wantPriceInput == "¥" || localtionInput == "")
                .foregroundColor(wantPriceInput == "" || wantPriceInput == "¥" || localtionInput == "" ? .gray : .accent)
            }
        }
        .onChange(of: addTradeRequester.requestStatus) { oldValue, newValue in
            if(oldValue == .requesting && newValue == .finish) {
                if(addTradeRequester.publishedResult) {
                    dismiss()
                }
            }
        }
    }
}

#Preview {
    AddTradePage(book: DefaultBook)
}
